<template>
    <div class="home-category">
      <ul class="menu">
        <li v-for="obj in brandList" :key="obj.item">
          <router-link to="/">{{ obj.name }}</router-link>
          <a href="javascript:;" v-for="item in obj.children.slice(0,2)" :key="item.id">{{ item.name }}</a>

          <!-- 弹层 -->
          <div class="layer" v-if="!obj.brands">
            <h4>分类推荐 <small>根据您的购买或浏览记录推荐</small></h4>
            <ul>
              <li v-for="goods in obj.goods" :key="goods.id">
                <router-link :to="`/goods?id=${goods.id}`">
                  <img
                    :src="goods.picture"
                    alt=""
                  />
                  <div class="info">
                    <p class="name ellipsis-2">
                      {{ goods.name }}
                    </p>
                    <p class="desc ellipsis">{{ goods.desc }}</p>
                    <p class="price"><i>¥</i>{{ goods.price }}</p>
                  </div>
                </router-link>
              </li>
            </ul>
          </div>

        <!-- 弹层 -->
        <div class="layer" v-else>
          <h4>品牌推荐 <small>根据您的购买或浏览记录推荐</small></h4>
          <!-- 循环品牌商品 -->
          <ul>
            <li class="brand" v-for="brand in obj.brands" :key="brand.id">
              <router-link to="/">
                <img
                  :src="brand.picture"
                  alt=""
                />
                <div class="info">
                  <p class="place"><i class="iconfont icon-dingwei"></i>{{ brand.place }}</p>
                  <p class="name ellipsis">{{ brand.nameEn }}</p>
                  <p class="desc ellipsis-2">{{ brand.desc }}</p>
                </div>
              </router-link>
            </li>
          </ul>
        </div>
        </li>
      </ul>
      <template v-if="brandList.length === 0">
        <!-- 结构要和真实数据一致(所以是10行) -->
      <ul class="menu" >
        <li v-for="i in 10" :key="i">
          <xtx-skeleton width="40px" height="18px" bg="rgba(255,255,255,0.2)" />
          <xtx-skeleton width="70px" height="18px" bg="rgba(255,255,255,0.2)" />
          <xtx-skeleton width="70px" height="18px" bg="rgba(255,255,255,0.2)" />
        </li>
      </ul>
      </template>
    </div>
  </template>

<script>
import { useStore } from 'vuex'
import { reactive, computed } from 'vue'
import { brandListApi } from '@/api'
export default {
  name: 'HomeCategory',
  setup () {
    // 获取vuex store
    const store = useStore()
    // 创建一个新对象
    const brand = reactive({
      id: '-1',
      name: '品牌',
      children: [{ name: '品牌推荐' }],
      brands: []
    })
    // 调用热门品牌接口
    const getList = async () => {
      const res = await brandListApi({ limit: 6 })
      // 将需要的结果赋值给brand对象
      brand.brands = res.result
    }
    getList()
    // 将新对象添加进list
    const brandList = computed(() => {
      // 浅拷贝 不会影响一级导航栏
      const list = [...store.state.category.list]
      if (list.length > 1) {
        list.push(brand)
      }
      return list
    })
    return {
      brandList
    }
  }
}
</script>

  <style scoped lang="less">
  .home-category {
    width: 250px;
    height: 500px;
    background: rgba(0, 0, 0, 0.8);
    position: relative;
    z-index: 99;
    .menu {
      // 分类下属商品用
      > li {
        padding-left: 40px;
        height: 50px;
        line-height: 50px;
        &:hover {
          background: var(--xtx-color);
        }
        .layer {
          width: 990px;
          height: 500px;
          background: rgba(255, 255, 255, 0.8);
          position: absolute;
          left: 250px;
          top: 0;
          display: none;
          padding: 0 15px;
          h4 {
            font-size: 20px;
            font-weight: normal;
            line-height: 80px;
            small {
              font-size: 16px;
              color: #666;
            }
          }
          ul {
            display: flex;
            flex-wrap: wrap;
            li {
              width: 310px;
              height: 120px;
              margin-right: 15px;
              margin-bottom: 15px;
              border: 1px solid #eee;
              border-radius: 4px;
              background: #fff;
              &:nth-child(3n) {
                margin-right: 0;
              }
              a {
                display: flex;
                width: 100%;
                height: 100%;
                align-items: center;
                padding: 10px;
                &:hover {
                  background: #e3f9f4;
                }
                img {
                  width: 95px;
                  height: 95px;
                }
                .info {
                  padding-left: 10px;
                  line-height: 24px;
                  width: 190px;
                  .name {
                    font-size: 16px;
                    color: #666;
                  }
                  .desc {
                    color: #999;
                  }
                  .price {
                    font-size: 22px;
                    color: var(--price-color);
                    i {
                      font-size: 16px;
                    }
                  }
                }
              }
            }
          }
        }
        &:hover {
          .layer {
            display: block;
          }
        }
        a {
          margin-right: 4px;
          color: #fff;
          &:first-child {
            font-size: 16px;
          }
        }
      }

      // 热门品牌推荐用
      li.brand {
        height: 180px;
        a {
          align-items: flex-start;
          img {
            width: 120px;
            height: 160px;
          }
          .info {
            p {
              margin-top: 8px;
            }
            .place {
              color: #999;
            }
          }
        }
      }
    }
  }
  </style>
